package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.COUNT_AMOUNT_PRODUCT_ORDER_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.FIND_PRODUCT_ORDER_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_CANCEL_PRODUCT_ORDER_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_PRODUCT_ORDER_LIST_PAGING_ERROR;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.ProductOrderService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.User;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;

/**
 * 订单管理接口
 * @author
 * @date 2023-09-06 14:50
 * @descriprion
 */
@Api(tags = "PRODUCT_ORDER_TAG")
@RestController
@RequestMapping("order")
public class ProductOrderController extends BaseController {

    @Autowired
    private ProductOrderService productOrderService;

    @ApiOperation(value = "queryProductOrderListPaging", notes = "QUERY_PRODUCT_ORDER_LIST_PAGING_NOTES")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderType", value = "ORDER_TYPE", dataType = "Int"),
            @ApiImplicitParam(name = "payModel", value = "PAY_MODEL", dataType = "Int"),
            @ApiImplicitParam(name = "taskStatus", value = "TASK_STATUS", dataType = "Int"),
            @ApiImplicitParam(name = "payStatus", value = "PAY_STATUS", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "PAGE_SIZE", required = true, dataType = "Int", example = "10"),
            @ApiImplicitParam(name = "pageNo", value = "PAGE_NO", required = true, dataType = "Int", example = "1")
    })
    @GetMapping()
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_PRODUCT_ORDER_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result queryProductOrderListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                              @RequestParam(value = "orderType", required = false) Integer orderType,
                                              @RequestParam(value = "payModel", required = false) Integer payModel,
                                              @RequestParam(value = "taskStatus", required = false) Integer taskStatus,
                                              @RequestParam(value = "payStatus", required = false) Integer payStatus,
                                              @RequestParam("pageSize") Integer pageSize,
                                              @RequestParam("pageNo") Integer pageNo) {
        Result result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        result = productOrderService.queryProductOrderListPaging(loginUser, pageSize, pageNo, orderType, payModel,
                taskStatus, payStatus);
        return result;
    }

    @ApiOperation(value = "getProductOrder", notes = "QUERY_PRODUCT_ORDER_DETAIL_NOTES")
    @GetMapping(value = "/getProductOrder")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(FIND_PRODUCT_ORDER_ERROR)
    public Result getProductOrder(@RequestParam(value = "id") int id) throws Exception {
        return productOrderService.getProductOrder(id);
    }

    @ApiOperation(value = "cancelProductOrder", notes = "CANCEL_PRODUCT_ORDER_NOTES")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Int")
    })
    @GetMapping(value = "/cancelProductOrder")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_CANCEL_PRODUCT_ORDER_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result cancelProductOrder(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                     @RequestParam(value = "id") Integer id) throws Exception {
        return productOrderService.cancelProductOrder(loginUser, id);
    }

    @ApiOperation(value = "countProductOrder", notes = "COUNT_AMOUNT_PRODUCT_ORDER_NOTES")
    @GetMapping(value = "/countProductOrder")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(COUNT_AMOUNT_PRODUCT_ORDER_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result countProductOrder(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                    @RequestParam(value = "type") Integer type,
                                    @RequestParam(value = "startDate") String startDate,
                                    @RequestParam(value = "endDate") String endDate) throws Exception {

        SimpleDateFormat format = new SimpleDateFormat(Constants.YYYY_MM_DD);
        Date temp = format.parse(endDate + "-01");
        temp = DateUtils.getLastDayOfMonth(temp);
        return productOrderService.countProductOrder(loginUser, type, format.parse(startDate + "-01"), temp);
    }

    @ApiOperation(value = "top3", notes = "COUNT_AMOUNT_PRODUCT_ORDER_NOTES")
    @GetMapping(value = "/top3")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(COUNT_AMOUNT_PRODUCT_ORDER_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "operation:overview:list")
    public Result top3(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(Constants.YYYY_MM_DD);
        return productOrderService.countProductOrderTop3(loginUser);
    }
}
